package com.xinyue.game.center.account.logic;

import com.xinyue.game.center.account.dao.AccountDao;
import com.xinyue.game.center.account.dao.entity.Account;
import com.xinyue.game.center.account.params.LoginParam;
import com.xinyue.game.center.common.ServerErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 王广帅
 * @Date 2021/5/23 21:09
 */
@Service
public class AccountLoginService {
    @Autowired
    private AccountManager accountManager;
    @Autowired
    private AccountDao accountDao;

    public Account login(LoginParam loginParam) {
        accountManager.checkUsername(loginParam.getUsername());
        accountManager.checkPassword(loginParam.getPassword());
        Account account = accountDao.findByUsername(loginParam.getUsername());
        checkAccountExist(account, loginParam);
        return account;
    }

    private void checkAccountExist(Account account, LoginParam loginParam) {
        if (account == null) {
            throw ServerErrorCode.ACCOUNT_1006.exception();
        }
        if (!account.getPassword().equals(loginParam.getPassword())) {
            throw ServerErrorCode.ACCOUNT_10087.exception();
        }
    }
}
